package algorithm;

import java.util.HashMap;
import java.util.Map;

public class T00205 {
    public static void main(String[] args) {
        String s1 = "ab";
        String s2 = "aa";
        System.out.println(new Solution().isIsomorphic(s1, s2));
    }

    static class Solution {
        public boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) return false;
            Map<Character, Character> map1 = new HashMap<>(s.length());
            Map<Character, Character> map2 = new HashMap<>(t.length());
            for (int i = 0; i < s.length(); i ++) {
                char cs = s.charAt(i);
                char ct = t.charAt(i);

                Character cache1 = map1.get(cs);
                if (cache1 == null) {
                    map1.put(cs, ct);
                } else if (cache1 != ct) {
                    return false;
                }

                Character cache2 = map2.get(ct);
                if (cache2 == null) {
                    map2.put(ct, cs);
                } else if (cache2 != cs) {
                    return false;
                }
            }
            return true;
        }
    }
}
